package scparser.factory.primitiveFactory;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.vecmath.Point3d;
import raycast.Scene;
import scparser.ParserHelper;
import lib.common.Constants;
import model.primitive.Primitive;
import model.primitive.Sphere;

public class SphereFactory extends PrimitiveFactory {
	private Point3d c;
	private Double r;
	

	public SphereFactory(Scene scene) {
		super.scene = scene;
	}

	@Override
	public Primitive create(List<String> block)
			throws PrimitiveFactoryException {
		String line = null;
		Iterator<String> it = block.iterator();
		try {
			while (it.hasNext()) {
				line = it.next().trim();
				
				if (line.endsWith(Constants.S_CLOSE)) {
					break;
				}
				
				if (line.startsWith("name")) {
					this.name = ParserHelper.takeString(line, "name");
				}
				
				if (line.startsWith("shader")) {
					String shaderName = ParserHelper.takeString(line, "shader");
					if ((this.shader = super.getShader(shaderName)) == null) {
						throw new PrimitiveFactoryException("shader " + shaderName + " not found");
					}			
				}
				
				if (line.startsWith("c")) {
					this.c = ParserHelper.takePoint(line, "c");
				}
				
				if (line.startsWith("r")) {
					this.r = ParserHelper.takeDouble(line, "r");
				}
				
				
			}
		} catch (IOException e) {
			throw new PrimitiveFactoryException(e.getMessage());
		} catch (NumberFormatException e) {
			throw new PrimitiveFactoryException(e.getMessage());
		}

		return new Sphere(super.name, super.shader.getName(), this.c, this.r);
	}

}
